const uniID = require('uni-id-common')
const {
	update
} = require('../../../../../../../../Users/li/Documents/Code/haibei/uniCloud-aliyun/cloudfunctions/publish/index.obj')

let dbJQL = null
let userCollection = null
module.exports = {
	_before: function() {
		const token = this.getUniIdToken()
		if (!token) {
			throw new Error('token不存在')
		}
		const clientInfo = this.getClientInfo()
		dbJQL = uniCloud.databaseForJQL({
			clientInfo
		})
		userCollection = dbJQL.collection('uni-id-users')
	},
	async getInfo() {
		try {
			const {data: userData} = await userCollection.where({
				_id: dbJQL.getCloudEnv('$cloudEnv_uid')
			}).field("nickname,avatar").get()
			const publishData = await dbJQL.collection('publish-record').groupBy('type')
				.groupField('count(*) as total')
				.get()
			return {
				errCode: 0,
				data: {
					userInfo: userData[0],
					publishInfo: publishData.data
				},
				errMsg: '更新成功'
			}
		} catch (e) {
			throw new Error(e.message)
		}
	},
	async update(params = {}) {
		try {
			await userCollection.where({
				_id: dbJQL.getCloudEnv('$cloudEnv_uid')
			}).update({
				nickname: params.nickname,
				avatar: params.avatar
			})
			return {
				errCode: 0,
				data: null,
				errMsg: '更新成功'
			}
		} catch (e) {
			throw new Error(e.message)
		}
	}
}